﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    public class TestDelegate
    {
        public class EventSender
        {
            public event EventHandler<ProgressEventArgs> ProgressEvent;
            public void TestProgressMessage()
            {
                EventHandler<ProgressEventArgs> localHandler = ProgressEvent;
                if (localHandler != null)
                {
                    localHandler(this, new ProgressEventArgs(1, 10, "TestMessage"));
                }
            }

            public void TestProgressMessage100()
            {
                EventHandler<ProgressEventArgs> localHandler = ProgressEvent;
                if (localHandler != null)
                {
                    for (int i = 0; i < 200; i++)
                    {
                        localHandler(this, new ProgressEventArgs(i, 200, "TestMessage"));
                    }
                }
            }

        }

        private string m_EventPercent;
        private string m_EventMessage;

        [Test]
        public void TestDelegateFunction()
        {
            EventSender oEventSender = new EventSender();
            oEventSender.ProgressEvent += this.EventReceived;
            oEventSender.TestProgressMessage();
            Assert.IsTrue(m_EventPercent.CompareTo("10") == 0, "Event Args should return percent 0");
            Assert.IsTrue(m_EventMessage.CompareTo("TestMessage") == 0, "Event Message should return TestMessage");
        }
        private void EventReceived(object source, ProgressEventArgs args)
        {
            this.m_EventPercent = args.Percent;
            this.m_EventMessage = args.Message;
            //System.Console.WriteLine(args.Percent);
        }
        [Test]
        public void TestDelegate100Percent()
        {
            this.m_EventPercent = null;
            EventSender oEventSender = new EventSender();
            oEventSender.ProgressEvent += this.EventReceived100;
            oEventSender.TestProgressMessage100();
            Assert.IsNull(this.m_EventPercent, "Percent receieved should return 10, not 10.5");
        }

        private void EventReceived100(object source, ProgressEventArgs args)
        {
            //System.Console.WriteLine(args.Percent);
            if (args.Percent.Contains("."))
                this.m_EventPercent = "Percent receieved should return 10, not 10.5";
        }
    }
}
